//
//  SizeCell.m
//  HDYS
//
//  Created by kaynewbie on 15/4/24.
//  Copyright (c) 2015年 kay. All rights reserved.
//

#import "SizeCell.h"

#define SCREEN_WIDTH [UIScreen mainScreen].bounds.size.width
#define BTNSPACE 10

@interface SizeCell ()

@property (nonatomic, strong) UIButton *preBtn;

@end

@implementation SizeCell

-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self createSubviews];
    }
    return self;
}

-(void)createSubviews{
    UILabel *titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 10, 40, 20)];
    titleLabel.text = @"尺码";
    titleLabel.font = [UIFont systemFontOfSize:14];
    [self.contentView addSubview:titleLabel];
    
    NSArray *array = @[@"S",@"M",@"L",@"XL"];
    CGFloat btnW = (SCREEN_WIDTH - (array.count + 1) * BTNSPACE) / array.count;
    for (int i = 0; i < array.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        btn.frame = CGRectMake(BTNSPACE + (btnW + BTNSPACE) * i, titleLabel.frame.origin.y + titleLabel.frame.size.height + BTNSPACE, btnW, 30);
        btn.tag = 666 + i;
        [btn setTitle:array[i] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:@"bg"] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.contentView addSubview:btn];
    }
}

-(void)btnClick:(UIButton *)btn {
    [self.preBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
    self.preBtn = btn;
    
//    NSLog(@"%@",btn.titleLabel.text);
    if ([self.delegate respondsToSelector:@selector(chooseSize:)]) {
        [self.delegate chooseSize:btn.titleLabel.text];
    }
}

@end
